package com.aduerr.v1shouyin.mvp.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.UseKouBeiCardEntity;
import com.aduerr.v1shouyin.http.APIRetrofit;
import com.aduerr.v1shouyin.http.RXRequest;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import com.aduerr.v1shouyin.util.ToastUtils;
import com.blankj.utilcode.util.StringUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.internal.observers.BlockingBaseObserver;
import io.reactivex.schedulers.Schedulers;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.HashMap;

/**
 * 口碑核销详情页面
 * Created by fyf on 2018/6/5 0005.
 */

public class PublicPraiseDetailsActivity extends AppCompatActivity {

    public static final String TAG = "PublicPraiseDetailsActivity";

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_praise_name)
    TextView tvPraiseName;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_tiaojian)
    TextView tvTiaojian;
    @BindView(R.id.tv_confirm_hexiao)
    TextView tvConfirmHexiao;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_user_konw)
    TextView tvUserKonw;
    private String strCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_public_praise_details);
        ButterKnife.bind(this);
        initData();
    }

    @SuppressLint("LongLogTag")
    private void initData() {
        tvTitle.setText("口碑券详情");
        Intent intent = getIntent();
//        Log.e(TAG, "initData: " + intent.getStringExtra("TicketStatus"));
        tvPraiseName.setText(intent.getStringExtra("ItemName"));
        tvName.setText("商品现价:" + intent.getStringExtra("CurrentPrice"));
        tvTiaojian.setText("商品原价:" + intent.getStringExtra("OriginalPrice"));
        tvTime.setText(intent.getStringExtra("EffectDate") + "--" + intent.getStringExtra("ExpireDate"));
        tvUserKonw.setText(intent.getStringExtra("TicketStatusDesc"));
        String useType = intent.getStringExtra("TicketStatus");
        if(StringUtils.equals("EFFECTIVE", useType)) {
            tvConfirmHexiao.setVisibility(View.VISIBLE);
            tvTiaojian.setVisibility(View.VISIBLE);
            tvName.setText("商品现价:" + intent.getStringExtra("CurrentPrice"));
            tvTiaojian.setText("商品原价:" + intent.getStringExtra("OriginalPrice"));
        } else if(StringUtils.equals("USED", useType)) {
            tvConfirmHexiao.setVisibility(View.INVISIBLE);
            tvTiaojian.setVisibility(View.INVISIBLE);
            tvName.setText("该券已被消费，无法核销");
        } else if(StringUtils.equals("UN_EFFECTIVE", useType)) {
            tvConfirmHexiao.setVisibility(View.INVISIBLE);
            tvTiaojian.setVisibility(View.INVISIBLE);
            tvName.setText("未核销，但不可在该门店核销");
        } else if(StringUtils.equals("REFUNDED", useType)) {
            tvConfirmHexiao.setVisibility(View.INVISIBLE);
            tvTiaojian.setVisibility(View.INVISIBLE);
            tvName.setText("该券已被退款，无法核销");
        } else if(StringUtils.equals("USING", useType)) {
            tvConfirmHexiao.setVisibility(View.INVISIBLE);
            tvTiaojian.setVisibility(View.INVISIBLE);
            tvName.setText("核销处理中，无法核销");
        } else if(StringUtils.equals("REFUNDING", useType)) {
            tvConfirmHexiao.setVisibility(View.INVISIBLE);
            tvTiaojian.setVisibility(View.INVISIBLE);
            tvName.setText("退款处理中，无法核销");
        }
        strCode = intent.getStringExtra("strTicketID");
    }


    @OnClick({R.id.img_back, R.id.tv_confirm_hexiao})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_confirm_hexiao://确认核销
                useKouBeiCard(strCode);
                break;
        }

    }

    @SuppressLint("LongLogTag")
    private void useKouBeiCard(String strTicketID) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("TicketID", strTicketID);
        APIRetrofit.getAPIService().getUseKouBeiCard(RXRequest.getXingPosParams(hashMap
                , PublicPraiseDetailsActivity.this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BlockingBaseObserver<UseKouBeiCardEntity>() {
                    @Override
                    public void onNext(UseKouBeiCardEntity useKouBeiCardEntity) {
                        int success = useKouBeiCardEntity.getSuccess();
                        if(useKouBeiCardEntity.getSuccess() == 1) {
                            Log.e(TAG,
                                    "onNext: " + useKouBeiCardEntity.getData().getSiteUserName());
                            Intent intent = new Intent(PublicPraiseDetailsActivity.this
                                    , KouBeiResultActivity.class);
                            intent.putExtra("strTicketID",
                                    useKouBeiCardEntity.getData().getTicketCode());
                            intent.putExtra("SiteUserName",
                                    useKouBeiCardEntity.getData().getSiteUserName());
                            intent.putExtra("CurrentPrice",
                                    useKouBeiCardEntity.getData().getCurrentPrice());
                            intent.putExtra("ItemName",
                                    useKouBeiCardEntity.getData().getItemName());
                            intent.putExtra("OriginalPrice",
                                    useKouBeiCardEntity.getData().getOriginalPrice());
                            intent.putExtra("UseDate",
                                    useKouBeiCardEntity.getData().getUseDate());
                            Log.e(TAG,
                                    "onNext: " + useKouBeiCardEntity.getData().getUseDate());
                            startActivity(intent);
                            finish();
                        } else if(success == -100 | success == -200) {
                            //// TODO: 2017/12/9 0009 绑定guid  界面
                            startActivity(new Intent(PublicPraiseDetailsActivity.this,
                                    BangGuidActivity.class));
                            finish();
                        } else if(success == -300 || success == -2 || success == -3 || success == -6) {
                            //// TODO: 2017/12/9 0009 去登录的界面
                            com.blankj.utilcode.util.ToastUtils.showShortSafe(useKouBeiCardEntity.getErrMsg());
                            SharedPreferencesUtils.remove(PublicPraiseDetailsActivity.this, "autologin");
                            SharedPreferencesUtils.remove(PublicPraiseDetailsActivity.this, "remberpassword");
                            startActivity(new Intent(PublicPraiseDetailsActivity.this,
                                    LoginActivity.class));
                            finish();
                        } else if(success == -4) {
                            //没有成功
                            String erMsg = useKouBeiCardEntity.getErrMsg();
                            ToastUtils.showToast(PublicPraiseDetailsActivity.this,
                                    erMsg + "");
                        } else {
                            com.blankj.utilcode.util.ToastUtils.showShortSafe(useKouBeiCardEntity.getErrMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(e instanceof SocketTimeoutException) {
                            Toast.makeText(PublicPraiseDetailsActivity.this, "网络连接超时",
                                    Toast.LENGTH_SHORT).show();
                        } else if(e instanceof ConnectException) {
                            Toast.makeText(PublicPraiseDetailsActivity.this, "连接失败",
                                    Toast.LENGTH_SHORT).show();
                        } else if(e instanceof UnknownHostException) {
                            Log.e("onError:", "error:-----" + e.getMessage());//R
                            // .string.checknet +
                            Toast.makeText(PublicPraiseDetailsActivity.this, "检查网络状态",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Log.e("onError:", "error:" + e.getMessage());//R.string
                            // .checknet +
                            Toast.makeText(PublicPraiseDetailsActivity.this, "检查网络状态",
                                    Toast.LENGTH_SHORT).show();
                        }
                        com.blankj.utilcode.util.ToastUtils.showShortSafe(e.getMessage());
                    }
                });
    }

}
